package com.cg.design.graphicdesignpattern.visitor;

import java.util.Iterator;

/**
 * Visitor类的子类，作用是访问数据结构并显示元素一览
 *
 * @program: LeetCode->ListVisitor
 * @description: Visitor类的子类，作用是访问数据结构并显示元素一览
 * @author: cg
 * @create: 2022-03-26 21:07
 **/
public class ListVisitor extends Visitor {

    /**
     * 当前访问的文件夹名字
     */
    private String currentDir = "";

    @Override
    public void visit(File file) {
        System.out.println(currentDir + "/" + file);
    }

    @Override
    public void visit(Directory directory) {
        System.out.println(currentDir + "/" + directory);
        String saveDir = currentDir;
        currentDir = currentDir + "/" + directory.getName();
        Iterator<Entry> iterator = directory.iterator();
        while (iterator.hasNext()) {
            iterator.next().accept(this);
        }
        currentDir = saveDir;
    }
}
